home *** CD-ROM | disk | FTP | other *** search
- Path: su3.in.net!news
- From: poundss@in.net (Sam Pounds)
- Newsgroups: comp.lang.c
- Subject: Re: Help with simple code
- Date: Mon, 15 Jan 1996 23:49:13 GMT
- Organization: INTERNET Indiana
- Message-ID: <4deocm$4co@su3.in.net>
- References: <4dbak5$oij@ionews.io.org>
- NNTP-Posting-Host: pm4-01.in.net
- X-Newsreader: Forte Free Agent 1.0.82
-
- jgordon@io.org (John Gordon MacPherson) wrote:
-
- >Can anyone tell me what's wrong with this piece of code? I lifted it
- >straight from a textbook.
-
- >Here's the code:
-
- >/* Calculating compound interest */
- >#include <stdio.h>
- >#include <math.h>
-
- >main()
- >{
- > int year;
- > double amount, principal = 1000, rate = 0.5;
-
- > printf("%4s%21s\n", "Year", "Amount on deposit");
-
- > for (year = 1; year <= 10; year++) {
- > amount = principal * pow(1.0 + rate, year);
- > printf("%4d%21.2f\n", year, amount);
- > }
-
- > return 0;
- >}
- >______________________________________________________________
-
- >and here's the error:
-
- >In function `main':
- >undefined reference to `pow'
-
- >I don't understand. `pow' is a function, not a variable?
- >Can anyone tell me what's wrong?
-
- Make sure you have the correct "path" for "math.h"
- I think THAT is your problem.
-
- Below is a "TEST"power function.
-
-
- /* Calculating compound interest */
- #include <stdio.h>
-
- double power(double base, double n);
-
- int main()
- {
- int year;
- double amount, principal = 1000, rate = 0.5;
-
- printf("%4s%21s\n", "Year", "Amount on deposit");
-
- for (year = 1; year <= 10; year++) {
- /* amount = principal * pow(1.0 + rate, year); */
- amount = principal * power(1.0 + rate, year);
- printf("%4d%21.2f\n", year, amount);
- }
-
- return 0;
- }
-
- double power(double base, double n)
- {
- int i;
- double p;
-
- p = 1;
- for (i = 1; i <= n; ++i)
- p = p * base;
- return p;
- }
-
-
-